package com.lianxi.nmms.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.nmms.constant.ResponseCodeConstant;
import com.lianxi.nmms.dao.DeptMapper;
import com.lianxi.nmms.entity.Dept;
import com.lianxi.nmms.entity.Product;
import com.lianxi.nmms.entity.ProductType;
import com.lianxi.nmms.exception.DeptStatusErrorException;
import com.lianxi.nmms.exception.ProductTypeExistException;
import com.lianxi.nmms.exception.RequestParameterErrorException;
import com.lianxi.nmms.service.DeptService;
import com.lianxi.nmms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 翔哥
 * 2025年10月08日
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo, String pageSize) {
        ModelAndView mav =new ModelAndView();
        PageInfo<Dept> deptPage =deptService.findPage(pageNo, pageSize);
        mav.addObject("deptPage",deptPage);
        mav.setViewName("backend/deptManager");
        return mav;
    }

    @RequestMapping("/addDept")
    public ResponseResult addDept(String name, String remark, HttpSession session){
        ResponseResult result =new ResponseResult();
        try {
            deptService.addDept(name, remark,session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }
    @RequestMapping("/addSonDept")
    public ResponseResult addSonDept(String name, String remark, String fatherDeptId,HttpSession session){
        ResponseResult result =new ResponseResult();
        try {
            deptService.addSonDept(name, remark,fatherDeptId,session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/findDeptById")
    public ResponseResult findDeptById(String id){
        ResponseResult result =new ResponseResult();
        try {
            Dept dept =  deptService.findDeptById(id);
            result.setMessage("成功");
            result.setReturnObject(dept);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }
    @RequestMapping("/updateDept")
    public ResponseResult updateDept(String id,String name, String remark) {
        ResponseResult result = new ResponseResult();
        try {
            deptService.updateDept(id,name,remark);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/enableDept")
    public ResponseResult enableDept(String id){
        ResponseResult result = new ResponseResult();

        try {
            deptService.enableDept(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            throw new RuntimeException(e);
        } catch (DeptStatusErrorException e) {
            result.setMessage(e.getMessage());
        }


        return result;
    }

    @RequestMapping("/disableDept")
    public ResponseResult disableDept(String id){
        ResponseResult result = new ResponseResult();
        try {
            deptService.disableDept(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
}
